package LowOrderSortingAlgorithm;

import java.util.Arrays;

public class InsertionSort {

    // 插入排序:时间复杂度O(N^2):空间复杂度O(1)
    // 具有稳定性
    public static void insertionSort(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        for (int i = 1; i < arr.length; i++) {
            for (int j = i - 1; j >= 0 && arr[j] > arr[j + 1]; j--) {
                swap(arr, j, j + 1);
            }
        }
        //System.out.println(Arrays.toString(arr));
    }

    public static void swap(int[] arr,int i,int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

//    public static void main(String[] args) {
//        int[] arr2 = new int[]{1,5,4,8,3,2,7,3,1,6,2,3,4,5,6,7,8,1};
//        System.out.println(Arrays.toString(arr2));
//        LowOrderSortingAlgorithm.BubbleSort.bubbleSort(arr2);
//        System.out.println(Arrays.toString(arr2));
//    }

}
